package com.ef.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.ClassType;
import com.ef.domain.ClassUnit;
import com.ef.domain.Course;
import com.ef.service.ClassTypeService;
import com.ef.service.CourseService;
import com.ef.util.CustomExample;

public class CourseAction extends BaseAction<Course> {

	private static final long serialVersionUID = 1L;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(CourseAction.class);

	private CourseService courseService;

	private ClassTypeService classTypeService;

	/**
	 * 可行的ClassType
	 */
	private List<ClassType> optionalClassTypeList;

	private List<ClassUnit> selectClassUnitList;

	@SuppressWarnings("serial")
	public String list() {
		this.listResult = courseService.findByExample(
				new CustomExample<Course>(this.getModel()) {
					public void appendCondition(Criteria criteria) {
						this.excludeProperty("hasNetCourse");
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	public String edit() {
		fetchListData();
		return EDIT;
	}

	public String selectCourseAjax() {
		fetchListData();
		StringBuffer rsp=new StringBuffer();
        for (ClassType type:optionalClassTypeList ){
        	rsp.append("<classtype>").append("{id:").append(type.getId()).append(",name:\"")
			.append(type.getName()).append("\"}</classtype>");
        }
        for (ClassUnit unit:selectClassUnitList ){
        	rsp.append("<classunit>").append("{id:").append(unit.getClassType().getId()).append(",name:\"")
			.append(unit.getClassType().getName()).append("\",limit:\"")
			.append(unit.getUpdateLimitTimes()).append("\"}</classunit>");
        }
        this.response(rsp.toString());
		return null;
	}

	private void fetchListData() {
		List<ClassType> allClassTypelist = classTypeService.findALLValid();
		if (this.getModel().getId() != null
				&& this.getModel().getClassUnits() != null) {
			selectClassUnitList = this.getModel().getClassUnits();
			for (ClassUnit cu : selectClassUnitList) {
				allClassTypelist.remove(cu.getClassType());
			}
			optionalClassTypeList = allClassTypelist;
		} else {
			optionalClassTypeList = allClassTypelist;
			selectClassUnitList = new ArrayList<ClassUnit>();
		}
	}

	public String save() {
		String classUnitString = getRequest().getParameter("classUnitString");
		ArrayList<ClassUnit> classUnits = null;
		if (classUnitString != null && classUnitString.trim().length() > 0) {
			classUnits = new ArrayList<ClassUnit>();
			String[] cidLimit = classUnitString.split(";");
			for (String cl : cidLimit) {
				String[] c = cl.split(",");
				ClassUnit unit = new ClassUnit();
				unit
						.setClassType(classTypeService.findById(Long
								.valueOf(c[0])));
				unit.setUpdateLimitTimes(Integer.valueOf(c[1].trim()));
				classUnits.add(unit);
			}
		}
		Course course = this.getModel();
		course.setClassUnits(classUnits);
		course.setUpdateTime(new Date());
		course.setUpdateUser(getSessionUser());
		courseService.saveOrUpdate(course);
		this.setResult(ConfirmMessage.Course);
		fetchListData();
		return EDIT;
	}

	public String delete() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
	 		courseService.delete(courseService.findById(Long.valueOf(i.trim())));
		}
		return DELETE;
	}

	public CourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public ClassTypeService getClassTypeService() {
		return classTypeService;
	}

	public void setClassTypeService(ClassTypeService classTypeService) {
		this.classTypeService = classTypeService;
	}

	public List<ClassType> getOptionalClassTypeList() {
		return optionalClassTypeList;
	}

	public void setOptionalClassTypeList(List<ClassType> optionalClassTypeList) {
		this.optionalClassTypeList = optionalClassTypeList;
	}

	public List<ClassUnit> getSelectClassUnitList() {
		return selectClassUnitList;
	}

	public void setSelectClassUnitList(List<ClassUnit> selectClassUnitList) {
		this.selectClassUnitList = selectClassUnitList;
	}

}
